package com.slwish.xty.utils;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.FragmentActivity;
import android.view.View;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.TimePickerView;
import com.othershe.nicedialog.SelectionDialog;
import com.othershe.nicedialog.ShareDialog;
import com.othershe.nicedialog.interf.DialogInterface;
import com.slwish.xty.entity.CityModel;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 模块说明：
 *
 * @author 陈浩
 * @date 2017/12/16
 */

public class DialogUtils {
    /**
     * @param mContext
     * @param arrays
     * @param onItemClickListener
     */
    public static void showBottomDialog(Context mContext, String[] arrays, DialogInterface.OnItemClickListener<SelectionDialog> onItemClickListener) {
        List<String> list = Arrays.asList(arrays);
        showBottomDialog(mContext, list, onItemClickListener);
    }

    /**
     * @param mContext
     * @param list
     * @param onItemClickListener
     */
    public static void showBottomDialog(Context mContext, List<String> list, DialogInterface.OnItemClickListener<SelectionDialog> onItemClickListener) {
        SelectionDialog.newInstance(mContext)
                .setDatas(list)
                .show(((FragmentActivity) mContext).getSupportFragmentManager())
                .setOnItemClickListener(onItemClickListener);
    }

    /**
     * 分享dialog
     * @param mContext
     * @param onItemClickListener
     */
    public static void showShareDialog(Context mContext,ShareDialog.ShareDialogClickListener onItemClickListener) {
        ShareDialog.newInstance(mContext)
                .show(((FragmentActivity) mContext).getSupportFragmentManager())
                .setShareDialogClickListener(onItemClickListener);
    }

    /**
     * 选择时间
     * @param mContext
     * @param listener
     */
    public static void showTimePickerDialog(Context mContext, final OnItemSelectListener listener) {
        //控制时间范围(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
        //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        startDate.set(1900, 0, 23);
        Calendar endDate = Calendar.getInstance();
//        endDate.set(2019, 11, 28);
        //时间选择器
        TimePickerView pvTime = new TimePickerView.Builder(mContext, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                // 这里回调过来的v,就是show()方法里面所添加的 View 参数，如果show的时候没有添加参数，v则为null
                if (listener != null)
                    listener.OnItemSelect(date, v);
            }
        })
                //年月日时分秒 的显示与否，不设置则默认全部显示
                .setType(new boolean[]{true, true, true, false, false, false})
                .setLabel("年", "月", "日", "时", "分", "秒")
                .isCenterLabel(false)
                .setDividerColor(Color.DKGRAY)
                .setContentSize(18)
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
//                .setBackgroundId(0x00FFFFFF) //设置外部遮罩颜色
                .setDecorView(null)
                .build();
        pvTime.show();
    }

    /**
     * 城市选择一级联动
     *
     * @param context
     * @param options1Items
     * @param listener
     */
    public static void showCityPickerView(Context context, List<CityModel> options1Items, final OnOptionSelectListener listener) {// 弹出选择器
        showCityPickerView(context, options1Items, null, listener);
    }

    /**
     * 城市选择二级联动
     *
     * @param context
     * @param options1Items
     * @param options2Items
     * @param listener
     */
    public static void showCityPickerView(Context context, List<CityModel> options1Items, List<ArrayList<String>> options2Items, final OnOptionSelectListener listener) {// 弹出选择器
        showCityPickerView(context, options1Items, options2Items, null, listener);
    }

    /**
     * 城市选择三级联动
     *
     * @param context
     * @param options1Items
     * @param options2Items
     * @param options3Items
     * @param listener
     */
    public static void showCityPickerView(Context context, List<CityModel> options1Items, List<ArrayList<String>> options2Items, List<ArrayList<ArrayList<String>>> options3Items, final OnOptionSelectListener listener) {// 弹出选择器

        OptionsPickerView pvOptions = new OptionsPickerView.Builder(context, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                if (listener != null)
                    listener.onOptionsSelect(options1, options2, options3, v);
                //返回的分别是三个级别的选中位置
            }
        })
                .setTitleText("城市选择")
                .setDividerColor(Color.BLACK)
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(20)
                .build();
        /*pvOptions.setPicker(options1Items);//一级选择器
        pvOptions.setPicker(options1Items, options2Items);//二级选择器*/
        pvOptions.setPicker(options1Items, options2Items, options3Items);//三级选择器
        pvOptions.show();
    }

    /**
     * 接口
     */
    public interface OnItemSelectListener {
        void OnItemSelect(Date time, View v);
    }

    /**
     * 接口
     */
    public interface OnOptionSelectListener {
        void onOptionsSelect(int position1, int position2, int position3, View v);
    }
}
